package com.acwing.chapter3;

import java.io.*;

/**
 * @author anan
 * @create 2021-08-27 12:40
 */
public class Shiwulian240 {
    private static int N = 50010;
    private static int cnt = 0;
    private static int[] p = new int[3*N];


    public static int find(int x){
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] str1 = reader.readLine().split(" ");
        int n = Integer.parseInt(str1[0]);
        int k = Integer.parseInt(str1[1]);
        for (int i = 1; i <= 3*n; i++) {
            p[i] = i;
        }

        while (k-- > 0){
            String[] str2 = reader.readLine().split(" ");
            int key = Integer.parseInt(str2[0]);
            int a = Integer.parseInt(str2[1]);
            int b = Integer.parseInt(str2[2]);

            if (a > n || b > n) cnt++;
            else if (key == 1){
                if (find(a) == find(b + n) || find(a) == find(b + n + n)) cnt++;
                else {
                    p[find(b)] = find(a);
                    p[find(b + n)] = find(a + n);
                    p[find(b + n + n)] = find(a + n + n);
                }
            }
            else if (key == 2){
                if (a == b || find(a) == find(b) || find(a + n + n) == find(b)) cnt++;
                else {
                    p[find(b)] = find(a + n);
                    p[find(b + n)] = find(a + n + n);
                    p[find(b + n + n)] = find(a);
                }
            }

        }
        writer.write(cnt + "\n");
        writer.flush();
        writer.close();
        reader.close();
    }
}
